package com.seadragon.apps.doctorcard.validator;

import static com.seadragon.apps.doctorcard.model.DoctorCardConstants.ERROR_CODE_INVALID_MESSAGE;
import static com.seadragon.apps.doctorcard.model.DoctorCardConstants.MESSAGE;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.seadragon.apps.doctorcard.model.Message;

@Component("messageValidator")
public class MessageValidator implements Validator {

	@Override
	public boolean supports(Class<?> arg0) {
		return Message.class.isAssignableFrom(arg0);
	}

	@Override
	public void validate(Object arg0, Errors arg1) {
		Message message = (Message) arg0;
		if ((message.getMediaContent() == null || message.getMediaContent().length() == 0)
			|| (message.getMediaType() == null || message.getMediaType().length() == 0)
			|| (message.getMediaIconUrl() == null || message.getMediaIconUrl().length() == 0)) {
			arg1.reject(MESSAGE, Integer.toString(ERROR_CODE_INVALID_MESSAGE));
		}
	}
}
